#include<iostream>
using namespace std;
#define LL long long

void exgcd(LL a,LL b,LL &d,LL &x,LL &y)
{
	if(b==0)
	{
		d=a;
		x=1;
		y=0;
	}
	else
	{
		exgcd(b,a%b,d,x,y);
		int temp=x;
		x=y;
		y=temp-a/b*y;
	}
}

LL solve(int k)
{
	LL a1,r1,a2,r2,a,b,c,x0,y0,d;
	bool flag=true;
	cin>>a1>>r1;
	for(int i=1;i<k;i++)
	{
		cin>>a2>>r2;
		a=a1,b=a2,c=r2-r1;
		exgcd(a,b,d,x0,y0);
		if(c%d!=0)
		{
			flag=false;
		}
		LL t=b/d;
		x0=(x0*(c/d)%t+t)%t;
		r1=a1*x0+r1;
		a1=a1*(a2/d);
	}
	if(!flag)
	{
		r1=-1;
	}
	return r1;
}

int main()
{
	int k;
	while(cin>>k)
	{
		cout<<solve(k)<<endl;
	}
	return 0;

}
